//let srcS = [ //---储存动画信息
//    {
//        id: "tail",
//        src: "common/images/tail2/tail ",
//        len: 22,
//        setInt: 90,
//        width: 720, //---默认宽高
//        height: 1280,
//        x: -10, //---调整画布误差
//        y: -30,
//        format: '.png'
//    },
//    {
//        id: "head",
//        src: "common/images/head3/head ",
//        len: 16,
//        interval: 55,
//        width: 720,
//        height: 1280,
//        x: 0,
//        y: 0,
//        format: '.png'
//    },
//]

export function preLoad(info, cb) { //--- offscreenCanvas在模拟器上有bug，暂时不跑通
    console.info('xxx--- preload start')
    let x = info.x, y = info.y , length = info.length, w = info.width, h = info.height
    let list=[]
    for (let i = 0; i < length; i++) {
        let bitmap;
        console.info('xxx--- preload src = '+info.images[i].src)
        let offscreen = new OffscreenCanvas(w,h);
        let offscreenCanvasCtx = offscreen.getContext('2d');
        offscreenCanvasCtx.drawImage(info.images[i], x, y, w, h);
        bitmap = offscreen.transferToImageBitmap();
        list.push(bitmap)
    }
    info.bitmap=list
    info.ok = true;
    cb(info);
}

export class playAnimation { //---创建一个动画对象---可以复用
    #ctx
    #id
    #info
    constructor(info) {
        this.#info = info
        this.#id = info.id
        console.info('xxx--- create playAnimation suc, id = '+info.id)
    }

    setCtx(ctx){
        this.#ctx = ctx
    }
    getId() {
        return this.#id;
    }
    play(w=this.#info.width, h=this.#info.height) {
        let Iv;
        let x=this.#info.x,y=this.#info.y
        console.info('xxx--- ok ? '+this.#info.ok)
        if (this.#info.ok) { //----已有离屏渲染的播放
            console.info('xxx--- playing offCanvas')
            return new Promise((resolve) => {
                let i = 0;
                let bitmap = this.#info.bitmap;
                Iv = setInterval(() => {
                    if (i < this.#info.length) {
                        if (i !== 0)this.#ctx.clearRect(x, y, w, h);
                        this.#ctx.transferFromImageBitmap(bitmap[i]);
                        ++i;
                    } else {
                        this.#ctx.clearRect(x, y, w, h)
                        clearInterval(Iv);
                        resolve("false");
                    }
                }, this.#info.interval);
            })
        }
        else //---无离屏渲染的播放
        return new Promise((resolve) => {
            console.info('xxx--- playing Canvas')
            var i = 0;
            Iv = setInterval(() => {
                if (i < this.#info.length) {
                    if (i == 0) {
                        this.#ctx.clearRect(x, y, w, h);
                        this.w = w;
                        this.h = h;
                    }
                    else {
                        this.#ctx.clearRect(x, y, w, h);
                    }
                    this.#ctx.drawImage(this.#info.images[i], x, y, w, h);

                    ++i;
                } else {
                    this.#ctx.clearRect(x, y, w, h);
                    //                        this.#ctx.drawImage(imgArray[0], this.x, this.y, this.w, this.h); //---可选择保留结尾动作
                    clearInterval(Iv);
                    resolve("false");
                }
            }, this.#info.interval);
        })
    }
    //    setInfo(infoList) { //---找到srcS中对应的动画对象
    //        this.info = infoList.find(e => e.id === this.id);
    //        if(this.info)console.info("find success")
    //        else console.info(this.id+" find lose")
    //    }
}
